//SPDX-FileCopyrightText: Copyright 2022-2024 深圳市同心圆网络有限公司
//SPDX-License-Identifier: GPL-3.0-only

package org_eval_dao

import (
	"context"

	"atomgit.com/openlinksaas/api-server/dao/db_ns"
	"atomgit.com/openlinksaas/proto-gen-go.git/org_evaluate_api"
	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/mongo"
	"go.mongodb.org/mongo-driver/mongo/options"
)

type EvaluateTargetInfoDbItem struct {
	TargetId      string `bson:"_id"`
	OrgId         string `bson:"orgId"`
	TargetName    string `bson:"targetName"`
	EvaluateCount uint32 `bson:"evaluateCount"`
	TimeStamp     int64  `bson:"timeStamp"`
}

func (item *EvaluateTargetInfoDbItem) ToEvaluateTargetInfo() *org_evaluate_api.EvaluateTargetInfo {
	return &org_evaluate_api.EvaluateTargetInfo{
		TargetId:      item.TargetId,
		TargetName:    item.TargetName,
		EvaluateCount: item.EvaluateCount,
	}
}

type _TargetDao struct {
	mc *mongo.Collection
}

func newTargetDao(db *mongo.Database) (*_TargetDao, error) {
	mc := db.Collection(db_ns.DB_NS_ORG_EVAL + "target")
	_, err := mc.Indexes().CreateOne(context.Background(), mongo.IndexModel{
		Keys: bson.D{{"orgId", 1}},
	})
	if err != nil {
		return nil, err
	}
	return &_TargetDao{mc: mc}, nil
}

func (db *_TargetDao) Insert(ctx context.Context, dbItem *EvaluateTargetInfoDbItem) error {
	_, err := db.mc.InsertOne(ctx, dbItem)
	return err
}

func (db *_TargetDao) Exist(ctx context.Context, orgId, targetId string) (bool, error) {
	count, err := db.mc.CountDocuments(ctx, bson.M{"_id": targetId, "orgId": orgId})
	return count > 0, err
}

func (db *_TargetDao) Get(ctx context.Context, orgId, targetId string) (*EvaluateTargetInfoDbItem, error) {
	retItem := &EvaluateTargetInfoDbItem{}
	err := db.mc.FindOne(ctx, bson.M{"_id": targetId, "orgId": orgId}).Decode(retItem)
	return retItem, err
}

func (db *_TargetDao) Remove(ctx context.Context, orgId, targetId string) error {
	_, err := db.mc.DeleteOne(ctx, bson.M{"_id": targetId, "orgId": orgId})
	return err
}

func (db *_TargetDao) RemoveByOrg(ctx context.Context, orgId string) error {
	_, err := db.mc.DeleteMany(ctx, bson.M{"orgId": orgId})
	return err
}

func (db *_TargetDao) List(ctx context.Context, orgId string) ([]*EvaluateTargetInfoDbItem, error) {
	cursor, err := db.mc.Find(ctx, bson.M{"orgId": orgId}, options.Find().SetSort(bson.M{"timeStamp": -1}))
	if err != nil {
		return nil, err
	}
	retList := []*EvaluateTargetInfoDbItem{}
	err = cursor.All(ctx, &retList)
	return retList, err
}

func (db *_TargetDao) CountById(ctx context.Context, orgId string, targetIdList []string) (uint32, error) {
	if len(targetIdList) == 0 {
		return 0, nil
	}
	count, err := db.mc.CountDocuments(ctx, bson.M{"orgId": orgId, "_id": bson.M{"$in": targetIdList}})
	return uint32(count), err
}

func (db *_TargetDao) UpdateName(ctx context.Context, orgId, targetId, targetName string) error {
	_, err := db.mc.UpdateOne(ctx, bson.M{"_id": targetId, "orgId": orgId}, bson.M{"$set": bson.M{"targetName": targetName}})
	return err
}

func (db *_TargetDao) UpdateEvaluateCount(ctx context.Context, orgId, targetId string, count uint32) error {
	_, err := db.mc.UpdateOne(ctx, bson.M{"_id": targetId, "orgId": orgId}, bson.M{"$set": bson.M{"evaluateCount": count}})
	return err
}
